package com.ibm.eg.cvmanager.dao.impl;

import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.ibm.eg.cvmanager.dao.util.HibernateUtil;
import com.ibm.eg.cvmanager.dto.INTERVIEW_STATUS;
import com.ibm.eg.cvmanager.dto.Interview;

import exception.TechnicalException;

/**
 * @author SAADA
 *
 */
public class InterviewDao extends BaseDao {
	
	private static final Log log = LogFactory.getLog(InterviewDao.class);

	public ArrayList<Interview> filterInterviews(Date startDate, Date endDate, int status) throws TechnicalException
	{
		Session session =  HibernateUtil.getCurrentSession();
		
		log.debug("filtering Interviews between dates with status");
		try {
			ArrayList<Interview> interviews = (ArrayList<Interview>) session
				.createCriteria(Interview.class)
				.add(Restrictions.between("interviewdate", startDate, endDate))
				.add(Restrictions.like("status", status))
				.list();
			log.debug("filter successful");
			
		
			return interviews;
		}
		catch(HibernateException ex)
		{
			log.error("filter failed", ex);
			throw new TechnicalException("filterInterviews(Date,Date,int) error in InterviewDao",ex);
		}
	}
	
	public ArrayList<Interview> filterInterviews(Date startDate, Date endDate) throws TechnicalException
	{
		Session session =  HibernateUtil.getCurrentSession();
		log.debug("filtering Interviews between dates");
		try {
			ArrayList<Interview> interviews = (ArrayList<Interview>) session
			.createCriteria(Interview.class)
			.add(Restrictions.between("interviewdate", startDate, endDate))
			.list();
			log.debug("filter successful");
			return interviews;
		}
		catch(HibernateException ex)
		{
			log.error("filter failed", ex);
			throw new TechnicalException("filterInterviews(Date,Date) error in InterviewDao",ex);
		}
	}
	
	public void markAsFailed(int interviewId) throws TechnicalException
	{
		int status = INTERVIEW_STATUS.FAILED.ordinal();
		Session session =  HibernateUtil.getCurrentSession();
		log.debug("Mark Interview as failed");
		try {
			ArrayList<Interview> interview = (ArrayList<Interview>) session
				.createCriteria(Interview.class)
				.add(Restrictions.eq("id", interviewId)).list();
			interview.get(0).setStatus(status);
			update(interview.get(0));
			log.debug("filter successful");
		}
		catch(HibernateException ex)
		{
			log.error("filter failed", ex);
			throw new TechnicalException("markAsFailed(int) error in InterviewDao",ex);
		}
	}
	
	public void markAsSucceeded(int interviewId) throws TechnicalException
	{
		int status = INTERVIEW_STATUS.SUCCEEDED.ordinal();
		Session session =  HibernateUtil.getCurrentSession();
		log.debug("Mark Interview as failed");
		try {
			ArrayList<Interview> interview = (ArrayList<Interview>) session
				.createCriteria(Interview.class)
				.add(Restrictions.eq("id", interviewId)).list();
			interview.get(0).setStatus(status);
			update(interview.get(0));
			log.debug("filter successful");
		}
		catch(HibernateException ex)
		{
			log.error("filter failed", ex);
			throw new TechnicalException("markAsSucceeded(int) error in InterviewDao",ex);
		}
	}
	public Interview getInterview(int id) throws TechnicalException
	{
		Session session =  HibernateUtil.getCurrentSession();
		log.debug("Getting interview");
		try {
			ArrayList<Interview> interview = (ArrayList<Interview>) session
				.createCriteria(Interview.class)
				.add(Restrictions.eq("id", id)).list();
			log.debug("success");
			return interview.get(0);
		}
		catch(HibernateException ex)
		{
			log.error("filter failed", ex);
			throw new TechnicalException("getInterview(int id) error in InterviewDao",ex);
		}
	}
}
